package com.ptkc.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NumberUtils {
    
  private static boolean isMatch(String regex, String orginal){ 
    if (orginal == null || orginal.trim().equals("")) { 
      return false; 
    } 
    Pattern pattern = Pattern.compile(regex); 
    Matcher isNum = pattern.matcher(orginal); 
    return isNum.matches(); 
  } 
  
  public static boolean isPositiveInteger(String orginal) { 
    return isMatch("^\\+{0,1}[1-9]\\d*", orginal); 
  } 
  
  public static boolean isNegativeInteger(String orginal) { 
    return isMatch("^-[1-9]\\d*", orginal); 
  } 
  
  public static boolean isWholeNumber(String orginal) { 
    return isMatch("[+-]{0,1}0", orginal) || isPositiveInteger(orginal) || isNegativeInteger(orginal); 
  } 
    
  public static boolean isPositiveDecimal(String orginal){ 
    return isMatch("\\+{0,1}[0]\\.[1-9]*|\\+{0,1}[1-9]\\d*\\.\\d*", orginal); 
  } 
    
  public static boolean isNegativeDecimal(String orginal){ 
    return isMatch("^-[0]\\.[1-9]*|^-[1-9]\\d*\\.\\d*", orginal); 
  } 
    
  public static boolean isDecimal(String orginal){ 
    return isMatch("[-+]{0,1}\\d+\\.\\d*|[-+]{0,1}\\d*\\.\\d+", orginal); 
  } 
    
  public static boolean isRealNumber(String orginal){ 
    return isWholeNumber(orginal) || isDecimal(orginal); 
  }

  public static boolean isNumeric(String str) {
    if (null == str || "".equals(str)) {
      return false;
    }
    String regx = "[+-]*\\d+\\.?\\d*[Ee]*[+-]*\\d+";
    Pattern pattern = Pattern.compile(regx);
    boolean isNumber = pattern.matcher(str).matches();
    if (isNumber) {
      return isNumber;
    }
    regx = "^[-\\+]?[.\\d]*$";
    pattern = Pattern.compile(regx);
    return pattern.matcher(str).matches();
  }
  
} 
